package net.zjitc.servlet.request.test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/5/15 10:00
 * Get请求相关操作：
 *  获取get请求的URL；请求方法；请求头User-Agent；请求参数
 **/
@WebServlet("/getServlet")
public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("获取Get请求的url和请求方法:");
        StringBuffer requestURL = req.getRequestURL();
        String method = req.getMethod();
        System.out.println(requestURL);
        System.out.println(method);

        System.out.println("获取请求头user-agent:");
        String agent = req.getHeader("User-Agent");
        System.out.println(agent);

        System.out.println("获取请求参数：");
        System.out.println("get请求的参数在url中?后面");
        String name = req.getParameter("name");
        System.out.println(name);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
